package ua.kharkov.knure.mikhaylov.st4.web.command;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.knure.mikhaylov.st4.db.entities.User;
import ua.kharkov.knure.mikhaylov.st4.db.enums.Role;
import ua.kharkov.knure.mikhaylov.st4.db.management.DBManager;
import ua.kharkov.knure.mikhaylov.st4.settings.LocaleData;
import ua.kharkov.knure.mikhaylov.st4.settings.Path;

/**
 * Command - logs user into the system
 * 
 * @author Saint
 * 
 */
public class LoginCommand extends Command {

	private static final long serialVersionUID = 3515883373979155849L;
	private static final Logger log = Logger.getLogger(LoginCommand.class);

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {

		log.debug("Command starts");

		HttpSession session = request.getSession(false);
		if (session != null)
			session.invalidate();

		session = request.getSession();

		// obtain login and password from the request
		DBManager manager = DBManager.getInstance();
		String login = request.getParameter("login");
		log.trace("Request parameter: loging --> " + login);

		String password = request.getParameter("password");

		// error handler
		String errorMessage = null;
		String forward = Path.PAGE_ERROR;

		if (login == null || password == null || login.isEmpty()
				|| password.isEmpty()) {
			errorMessage = LocaleData.getResourceBundle().getString(
					"servlet.error.data.empty");
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}

		User user = manager.findUserByLogin(login);
		log.trace("Found in DB: user --> " + user);

		if (user == null || !password.equals(user.getPassword())) {
			errorMessage = LocaleData.getResourceBundle().getString(
					"servlet.error.user.notfound");
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		} else {
			Role userRole = user.getRole();
			log.trace("userRole --> " + userRole);

			if (userRole == Role.ADMIN)
				forward = Path.COMMAND_BROWSE_ADMIN_PAGE;

			if (userRole == Role.CLIENT)
				forward = Path.COMMAND_BROWSE_USER_PAGE;

			session.setAttribute("user", user);
			log.trace("Set the session attribute: user --> " + user);

			session.setAttribute("userRole", userRole);
			log.trace("Set the session attribute: userRole --> " + userRole);

			log.info("User " + user + " logged as "
					+ userRole.toString().toLowerCase());
		}

		log.debug("Command finished");

		request.setAttribute("message", LocaleData.getResourceBundle()
				.getString("servlet.success.login"));
		request.setAttribute("next", forward);
		forward = Path.PAGE_SUCCESS;
		return forward;
	}

}